#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int n,m,c[N];
ll a[N],b[N];
bool islg[N];
int T;
ll X[4];
char d[22][N];
int win[N],tmp[N];
int main(){
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i<<=1)islg[i]=1;
    bool isA=1;
    for(int i=1;i<=n;i++)scanf("%lld",a+i);
    for(int i=1;i<=m;i++){
        scanf("%d",c+i);
        if(!islg[c[i]])isA=0;
    }
    int k=1;
    while((1<<k)<n)k++;
    for(int i=1;i<=k;i++){
        scanf("%s",d[i]+1);
    }
    if(isA){
        scanf("%d",&T);
        while(T--){
            for(int i=0;i<4;i++)scanf("%lld",X+i);
            for(int i=1;i<=n;i++)b[i]=(a[i]^X[i%4]);
            long long ans=0;
            for(int t=1;t<=m;t++){
                int num=c[t],cur=1;
                for(int i=1;i<=num;i++)win[i]=i;
                while(num>1){
                    for(int i=1;i<=num/2;i++){
                        if(d[cur][i]=='0'){
                            if(b[win[i*2-1]]>=cur)tmp[i]=win[i*2-1];
                            else tmp[i]=win[i*2];
                        }else{
                            if(b[win[i*2]]>=cur)tmp[i]=win[i*2];
                            else tmp[i]=win[i*2-1];
                        }
                    }
                    for(int i=1;i<=num/2;i++)win[i]=tmp[i];
                    cur++;
                    num>>=1;
                }
                ans^=(1LL*t*win[1]);
            }
            printf("%lld\n",ans);
        }
        return 0;
    }
	return 0;
}
/*
things to check:
1. freopen
2. int / long long
3. array bounds
4. clear when multi-tests
5. MOD
g++ arena.cpp -o arena -Wall -Wextra -Wshadow -Wconversion
*/